using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dune.Business.Managers;
using Edge.Common.ControlFlow;
using Edge.Common.Services;
using Edge.Common.ControlFlow;

namespace Edge.Common.Web.DataSourceAdapters
{
    public class FilteredDataSourceFromSession<TDalService, TEntity, TFilter> : FilteredDataSource<TDalService, TEntity, TFilter>
        where TDalService : IFilteredSearch<TEntity, TFilter>
        where TFilter : class, ICloneable, new()
    {

        public class SessionFilterHolder<TFilter> : IFilterHolder<TFilter>
            where TFilter : class, new()
        {
            Control targetControl;
            private TFilter template = null;

            public SessionFilterHolder(Control targetControl)
            {
                this.targetControl = targetControl;
            }

            public SessionFilterHolder(Control targetControl, TFilter template)
            {
                this.targetControl = targetControl;
                this.template = template;
            }


            public TFilter Filter
            {
                get
                {
                    HttpContext ctx = HttpContext.Current;
                    Assert.CheckNotNull(ctx);
                    if (ctx.Session[Key] == null)
                    {
                        if (template != null)
                            ctx.Session[Key] = template;
                        else
                            ctx.Session[Key] = new TFilter();
                    }
                    return (TFilter)ctx.Session[Key];
                }
            }

            protected string Key
            {
                get
                {
                    return targetControl.Page.GetType().Name + targetControl.ClientID;
                }
            }

        }

        public FilteredDataSourceFromSession(GridView gridView)
            : base(gridView, new SessionFilterHolder<TFilter>(gridView))
        {

        }

        private TFilter template = null;

        public FilteredDataSourceFromSession(GridView gridView, TFilter template)
            : base(gridView, new SessionFilterHolder<TFilter>(gridView, template))
        {
            this.template = template;
        }


        public TFilter Template
        {
            get { return template; }
        }

        protected override BaseDataSourceView CreateDataSourceView()
        {
            BaseDataSourceView ret = base.CreateDataSourceView();
            //ret.SelectExecuted += Ret_OnSelectExecuted;
            return ret;
        }

//        private void Ret_OnSelectExecuted(object sender, BaseDataSourceView.SelectedEventArgs e)
//        {
//            Locator.GetService<ISearchesRepository>().TakeSearch(
//                typeof(TEntity),
//                FilteredSearchInfoProvider<TDalService, TEntity, TFilter>.CreateSearchInfoProvider(e, Filter));
//        }
    }
}
